package com.itwxz.forum.controller.pc;


import com.itwxz.common.annotation.Log;
import com.itwxz.common.core.controller.BaseController;
import com.itwxz.common.core.domain.AjaxResult;
import com.itwxz.common.core.page.TableDataInfo;
import com.itwxz.common.enums.BusinessType;
import com.itwxz.common.utils.poi.ExcelUtil;
import com.itwxz.forum.domain.po.ForumPost;
import com.itwxz.forum.domain.vo.ForumPostVo;
import com.itwxz.forum.service.ForumPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 论坛帖子Controller
 *
 * @author gastronome
 * @date 2025-05-19
 */
@Tag(name = "论坛帖子Controller")
@RestController
@RequestMapping("/forum/post")
public class ForumPostController extends BaseController {
	@Autowired
	private ForumPostService forumPostService;

	/**
	 * 查询论坛帖子列表
	 */
	@Operation(summary = "查询论坛帖子列表")
	@PreAuthorize("@ss.hasPermi('forum:post:list')")
	@GetMapping("/list")
	public TableDataInfo list(ForumPost forumPost) {
		startPage();
		List<ForumPostVo> list = forumPostService.selectForumPostList(forumPost);
		return getDataTable(list);
	}

	/**
	 * 导出论坛帖子列表
	 */
	@Operation(summary = "导出论坛帖子列表")
	@PreAuthorize("@ss.hasPermi('forum:post:export')")
	@Log(title = "论坛帖子", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, ForumPost forumPost) {
		List<ForumPostVo> list = forumPostService.selectForumPostList(forumPost);
		ExcelUtil<ForumPostVo> util = new ExcelUtil<>(ForumPostVo.class);
		util.exportExcel(response, list, "论坛帖子数据");
	}

	/**
	 * 获取论坛帖子详细信息
	 */
	@Operation(summary = "获取论坛帖子详细信息")
	@PreAuthorize("@ss.hasPermi('forum:post:query')")
	@GetMapping(value = "/{postId}")
	public AjaxResult getInfo(@PathVariable("postId") Long postId) {
		return success(forumPostService.getById(postId));
	}

	/**
	 * 新增论坛帖子
	 */
	@Operation(summary = "新增论坛帖子")
	@PreAuthorize("@ss.hasPermi('forum:post:add')")
	@Log(title = "论坛帖子", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody ForumPost forumPost) {
		return toAjax(forumPostService.save(forumPost));
	}

	/**
	 * 修改论坛帖子
	 */
	@Operation(summary = "修改论坛帖子")
	@PreAuthorize("@ss.hasPermi('forum:post:edit')")
	@Log(title = "论坛帖子", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody ForumPost forumPost) {
		return toAjax(forumPostService.updateById(forumPost));
	}

	/**
	 * 删除论坛帖子
	 */
	@Operation(summary = "删除论坛帖子")
	@PreAuthorize("@ss.hasPermi('forum:post:remove')")
	@Log(title = "论坛帖子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
	public AjaxResult remove(@PathVariable Long[] postIds) {
		return toAjax(forumPostService.removeByIds(Arrays.asList(postIds)));
	}
}
